package org.streets.workflow.engine.task;

import org.streets.workflow.engine.ITaskInstance;
import org.streets.workflow.engine.WorkflowEngine;
import org.streets.workflow.engine.WorkflowException;
import org.streets.workflow.model.WFAutoTask;
import org.streets.workflow.model.WFProcess;
import org.streets.workflow.model.WFTask;
import org.streets.workflow.model.resource.WFApplication;


/**
 *
 * @author 非也
 * @version 1.0
 * Created on Mar 21, 2009
 */
public class DefaultToolTaskHandler implements ITaskHandler{

    public void handle(WorkflowEngine engine, ITaskInstance taskInstance) throws WorkflowException {
        
        if (!WFTask.AUTO.equals(taskInstance.getTaskType())) {
            throw new WorkflowException(taskInstance.getProcess(),
                    taskInstance.getActivityModel(),
                    "DefaultToolTaskInstanceRunner：TaskInstance的任务类型错误，只能为TOOL类型");
        }
        
        WFTask task = taskInstance.getTaskModel();
        
        if (task == null) {
            WFProcess process = taskInstance.getProcess().getModel();
            throw new WorkflowException(taskInstance.getProcess().getId(), process,
                    taskInstance.getTaskModelId(),
                    "The Task is null,can NOT start the taskinstance,");
        }
        
        WFApplication app = ((WFAutoTask) task).getApplication();
        if (app == null || app.getExecutor() == null) {
            WFProcess process = taskInstance.getProcess().getModel();
            throw new WorkflowException(taskInstance.getProcess().getId(), process,
                    taskInstance.getTaskModelId(),
                    "The task.getApplication() is null or task.getApplication().getHandler() is null,can NOT start the taskinstance,");
        }
        //FIXME dzb IOC处理？
        ITaskExecutor executor = null;
		try {
			executor = (ITaskExecutor) Class.forName(app.getExecutor()).newInstance();
		} catch (Exception e1) {
			e1.printStackTrace();
		}
        // Object obj = runtimeContext.getBeanByName(((WFAutoTask) task).getApplication().getHandler());

        if (executor == null){
            WFProcess process = taskInstance.getProcess().getModel();
            throw new WorkflowException(taskInstance.getProcess().getId(), process,
                    taskInstance.getTaskModelId(),
                    "Run tool task instance error! Not found the instance of "+((WFAutoTask) task).getApplication().getExecutor()+" or the instance not implements IApplicationHandler");
            
        }
        //执行自动任务
        try {
            executor.execute(taskInstance);
        } catch (Exception e) {            
            //TODO wmj2003 对tool类型的task抛出的错误应该怎么处理？ 这个时候引擎会如何？整个流程是否还可以继续？
        	e.printStackTrace();
            throw new WorkflowException(taskInstance.getProcess(),
                    taskInstance.getActivityModel(),
                    "DefaultToolTaskInstanceRunner：TaskInstance的任务执行失败！");
        }
        
        engine.completeTaskInstance(taskInstance, null);

    }

}
